﻿using System;
using System.Collections.Generic;
using System.Runtime.Remoting.Messaging;

namespace AEM.Data.BLL.Actions
{
    /// <summary>
    /// 处理电梯信息相关
    /// 与UI交互的类
    /// LYNX.KOR
    /// 2011-10-20
    /// </summary>
    public static class LiftInfo
    {
        static LiftInfo()
        {
            List = doList;
            AddLiftInfo = doAddLiftInfo;
            ModifiyLiftInfo = doModifiyLiftInfo;
            DeleteLiftInfo = doDeleteLiftInfo;
            ModifiyLiftMargin = doModifiyLiftMargin;
            ModifiyGroupMargin = doModifiyGroupMargin;
            DeleteAll = doDeleteAll;
        }

        #region 调用的函数
        private static List<Model.LiftInfo> doList(string condition)
        {
            return DAL.LiftInfo.List(condition);
        }

        private static AddLiftResult doAddLiftInfo(Model.LiftInfo args)
        {
            if (null == args) return AddLiftResult.LiftInfoIsNull;

            if (DAL.LiftInfo.Exist(args))
                return AddLiftResult.LiftNumAlreadyExist;

            bool ret = DAL.LiftInfo.Add(args);
            if (ret)
                return AddLiftResult.OK;
            else
                return AddLiftResult.Error;
        }

        private static ModifiyliftResult doModifiyLiftInfo(Data.Model.LiftInfo args)
        {
            if (null == args) return ModifiyliftResult.LiftInfoIsNull;
            Model.LiftInfo model = DAL.LiftInfo.Select(args.ID);
            if (null == model) return ModifiyliftResult.LiftInfoIsNull;
            if (model.LiftNum != args.LiftNum)
                if (DAL.LiftInfo.Exist(args)) return ModifiyliftResult.LiftNumAlreadyExist;

            bool ret = DAL.LiftInfo.Update(args);

            return ModifiyliftResult.OK;
        }

        private static DeleteliftResult doDeleteLiftInfo(int id)
        {
            if (!DAL.LiftInfo.Exist(id)) return DeleteliftResult.LiftIsNotExist;

            bool ret = DAL.LiftInfo.Delete(id);
            return DeleteliftResult.OK;
        }

        private static bool doDeleteAll()
        {
            return DAL.LiftInfo.Delete();
        }

        private static int doModifiyLiftMargin(int liftid, double left, double top)
        {
            Model.LiftInfo model = DAL.LiftInfo.Select(liftid);

            if (null != model)
            {
                model.Margin = left.ToString("0.0000") + "," + top.ToString("0.0000");
                bool ret = DAL.LiftInfo.Update(model);
                return 1;
            }
            return 0;
        }

        private static int doModifiyGroupMargin(int id, double left, double top)
        {
            Model.GroupMargin model = DAL.GroupMargin.Select("ID", id.ToString());
            if (null != model)
            {
                bool ret = DAL.GroupMargin.Update(id, left.ToString("0.0000") + "," + top.ToString("0.0000"));
                if (ret) return 0;
            }
            else
            {
                bool ret = DAL.GroupMargin.Add(id, left.ToString("0.0000") + "," + top.ToString("0.0000"));
                if (ret) return 0;
            }
            return -1;
        }
        #endregion 函数end

        #region 委托群

        public static Func<string, List<Model.LiftInfo>> List;

        /// <summary>
        /// 添加电梯信息
        /// </summary>
        public static Func<Model.LiftInfo, AddLiftResult> AddLiftInfo;

        /// <summary>
        /// 修改电梯信息
        /// </summary>
        public static Func<Data.Model.LiftInfo, ModifiyliftResult> ModifiyLiftInfo;

        public static Func<int, double, double, int> ModifiyLiftMargin;

        public static Func<int, double, double, int> ModifiyGroupMargin;

        /// <summary>
        /// 删除电梯信息
        /// 参数一:数据编号 ID
        /// 参数二:删除返回值
        /// </summary>
        public static Func<int, DeleteliftResult> DeleteLiftInfo;

        public static Func<bool> DeleteAll;

        /// <summary>
        /// 主界面电梯运行状态
        /// 参数一:电梯编号 LiftNum
        /// 参数二:电梯运行方向,true上行,false下行
        /// 参数三:当前楼层
        /// 参数四:电梯状态Data.Enum.LiftStatus
        /// </summary>
        public static Action<SerialDataEventArgs> LiftRun;

        public static void LiftRunCallback(IAsyncResult tag)
        {
            AsyncResult result = (AsyncResult)tag;
            Action<SerialDataEventArgs> del = (Action<SerialDataEventArgs>)result.AsyncDelegate;
            del.EndInvoke(tag);
        }
        /// <summary>
        /// 单台电梯监控
        /// 参数一:电梯编号 LiftNum
        /// 参数二:电梯运行方向,true上行,false下行
        /// 参数三:门状态 true开;false关
        /// 参数四:当前楼层
        /// 参数五:电梯状态Data.Enum.LiftStatus
        /// </summary>
        public static Action<SerialDataEventArgs> LiftMonitoring;
        public static Action<SerialDataEventArgs> EscalatorMonitoring;
        public static Action<SerialDataEventArgs> TrailsMonittoring;

        public static void MonitoringCallback(IAsyncResult tag)
        {
            AsyncResult result = (AsyncResult)tag;
            Action<SerialDataEventArgs> del = (Action<SerialDataEventArgs>)result.AsyncDelegate;
            del.EndInvoke(tag);
        }
        /// <summary>
        /// 同线路电梯运行状态
        /// 参数一:电梯编号 LiftNum
        /// 参数二:电梯状态Data.Enum.LiftStatus
        /// </summary>
        public static Action<SerialDataEventArgs> LiftRunInLine;
        public static Action<int> LiftRunNCInLine;

        /// <summary>
        /// 电梯组中电梯运行状态
        /// 参数一:电梯编号 LiftNum
        /// 参数二:电梯运行方向,true上行,false下行
        /// 参数三:门状态 true开;false关
        /// 参数四:当前楼层
        /// 参数五:内招楼层
        /// 参数六:Vip招唤楼层
        /// 参数七:电梯状态Data.Enum.LiftStatus
        /// </summary>
        public static Action<SerialDataEventArgs> LiftRunInGroup;

        public static Action<SerialDataEventArgs> LiftRunInGatherFire;

        /// <summary>
        /// 超时重置
        /// </summary>
        public static Action<int, int> LiftRunNC;

        /// <summary>
        /// 各种界面信息修改后调用
        /// </summary>
        public static Action ReLoadUI;
        public static Action ResetConnectionState;

        public static Action ResetAlarm;
        #endregion 委托群end

        #region 枚举群

        public enum AddLiftResult
        {
            OK,
            LiftInfoIsNull,
            LiftNumAlreadyExist,
            Error
        }

        public enum ModifiyliftResult
        {
            OK,
            LiftInfoIsNull,
            LiftIsNotExist,
            LiftNumAlreadyExist,
            LiftIsExist
        }

        public enum DeleteliftResult
        {
            OK,
            LiftIsNotExist
        }
        #endregion 枚举群end

    }
}
